package com.luminarepo.system.controller;

import com.luminarepo.common.model.vo.ResultVO;
import com.luminarepo.system.model.Article;
import com.luminarepo.system.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 根据ID获取文章信息
     */
    @GetMapping("/{id}")
    public ResultVO<Article> getArticleById(@PathVariable("id") Long id) {
        Article article = articleService.getById(id);
        return article != null ? ResultVO.ok(article) : ResultVO.fail("文章不存在");
    }

    /**
     * 获取所有文章信息
     */
    @GetMapping("/list")
    public ResultVO<List<Article>> listAllArticles() {
        List<Article> articles = articleService.list();
        return ResultVO.ok(articles);
    }

    /**
     * 删除指定文章
     */
    @DeleteMapping("/{id}")
    public ResultVO deleteArticleById(@PathVariable("id") Long id) {
        boolean result = articleService.removeById(id);
        return result ? ResultVO.ok("文章删除成功") : ResultVO.fail("文章删除失败");
    }

    /**
     * 批量保存或更新文章
     */
    @PostMapping("/saveOrUpdateBatch")
    public ResultVO saveOrUpdateBatch(@RequestBody List<Article> articles) {
        boolean result = articleService.saveOrUpdateBatch(articles);
        return result ? ResultVO.ok("文章批量保存或更新成功") : ResultVO.fail("文章批量保存或更新失败");
    }
}
